class RecordsController < ApplicationController
#  before_filter :find_project,  :except => [:index, :new, :create]  
  before_filter :find_customer_user,  :only => [:show, :new, :edit,:create,:update]  

  layout "gestion"
  
  access_control do
    allow :team_leader
    allow :user
    allow :chief_of_staff
  end
  
  # GET /records
  # GET /records.xml
  def index
    @records = Record.all
    @project = Project.all
    @tasks = Task.all


    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @records }
    end
  end

  # GET /records/1
  # GET /records/1.xml
  def show
    @record = Record.find(params[:id])
    @project = Project.find(@record.project_id)
    @task = Task.find(@record.task_id)
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @record }
    end
  end

  # GET /records/new
  # GET /records/new.xml
  def new
    @record = Record.new
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @record }
    end
  end

  # GET /records/1/edit
  def edit
    @record = Record.find(params[:id])
  end

  # POST /records
  # POST /records.xml
  def create
    @record = Record.new(params[:record])
    @record.employee_id = @current_user.id
#    @record = Record.create(params[:record])
#    flash[:notice]=@record.employee_id# = @current_user.user_id
    
    respond_to do |format|
      if @record.save
        flash[:notice] = 'Record was successfully created.'
        format.html { redirect_to(@record) }
        format.xml  { render :xml => @record, :status => :created, :location => @record }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @record.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /records/1
  # PUT /records/1.xml
  def update
    @record = Record.find(params[:id])

    respond_to do |format|
      if @record.update_attributes(params[:record])
        flash[:notice] = 'Record was successfully updated.'
        format.html { redirect_to(@record) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @record.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /records/1
  # DELETE /records/1.xml
  def destroy
    @record = Record.find(params[:id])
    @record.destroy

    respond_to do |format|
      format.html { redirect_to(records_url) }
      format.xml  { head :ok }
    end
  end
  
private 

	def find_project 
		@project = Project.find(params[:id])  
	end 

	def find_customer_user 
		 @customers = Customer.all
    @employees = User.all
    @projects = Project.all
    @tasks = Task.all
	end 
  
end
